package com.achievo.know.domain;

import java.io.File;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.apache.commons.io.FilenameUtils;

import com.achievo.know.component.impl.FileManagerConfig;
import com.achievo.know.pojo.Constants;
import com.achievo.know.pojo.ImageZipUtil;
import com.dayatang.domain.BaseEntity;
import com.dayatang.domain.InstanceFactory;
import sun.misc.BASE64Encoder;

/**
 * @author hp
 *
 */
@Entity
@Table(name="RAMS_FILE_ATTACHMENT")
public class FileAttachment extends BaseEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3151273970080801687L;
	
	//唯一标识
	@NotNull
	@Column(name="GUID",length=32,nullable=false)
	private String guid;

	//附件标题
	@NotNull
	@Column(name="ATTACH_NAME",length=500,nullable=false)
	private String attachName;
	
	//附件路径
	@NotNull
	@Column(name="ATTACH_PATH",length=100,nullable=false)
	private String attachPath;
	
	//附件大小
	@Column(name="ATTACH_SIZE",length=3)
	private int attachSize;
	
	//附件类型
	@Embedded
	private AttachmentType attachType;
	
	@NotNull
	@Column(name="FK_ID",nullable=false)
	private Long fkId;
	
	@NotNull
	@Column(name="FK_TYPE",nullable=false)
	private int fkType;
	
	//@OneToOne
	/*@ManyToOne(cascade=CascadeType.REFRESH,optional=false)  
    @JoinColumn(name = "FK_FILE_ID")  
	private File file;*/
	
	@Column(name="CONVERT_OK")
	private boolean convertOk;
	
	@Column(name="CHECK_SUM")
	private long checkSum;
	
	@Column(name="PHOTO_NAME",length=36)
	private String photoName;
	
	@Column(name="DOWN_SUM")
	private int downSum;
	
	@Column(name="CONVERT_ERROR",length=1000)
	private String convertError;

    @Transient
    private String encodeId;

    public String getEncodeId() {
        BASE64Encoder encoder=new BASE64Encoder();
        return encoder.encode((id+"").getBytes());
    }

    public String getConvertError() {
		return convertError;
	}

	public void setConvertError(String convertError) {
		this.convertError = convertError;
	}

	public int getDownSum() {
		return downSum;
	}

	public void setDownSum(int downSum) {
		this.downSum = downSum;
	}

	public String getPhotoName() {
		return photoName;
	}

	public void setPhotoName(String photoName) {
		this.photoName = photoName;
	}


	public long getCheckSum() {
		return checkSum;
	}

	public void setCheckSum(long checkSum) {
		this.checkSum = checkSum;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getAttachName() {
		return attachName;
	}

	public void setAttachName(String attachName) {
		this.attachName = attachName;
	}

	public String getAttachPath() {
		return attachPath;
	}

	public void setAttachPath(String attachPath) {
		this.attachPath = attachPath;
	}

	public int getAttachSize() {
		return attachSize;
	}

	public void setAttachSize(int attachSize) {
		this.attachSize = attachSize;
	}

	public AttachmentType getAttachType() {
		return attachType;
	}

	public void setAttachType(AttachmentType attachType) {
		this.attachType = attachType;
	}
	

	public Long getFkId() {
		return fkId;
	}

	public void setFkId(Long fkId) {
		this.fkId = fkId;
	}
	
	

	public int getFkType() {
		return fkType;
	}

	public void setFkType(int fkType) {
		this.fkType = fkType;
	}

	@Override
	public boolean equals(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return 0;
	}

	/*public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}*/
	
	public boolean isCanPreview(){
		if(this.getAttachType()!=null){
			if(isImage())
				return true;
			if(isText())
				return true;
			if(isConvertedSwf())
				return true;
			if(isSwf())
				return true;
			if(isPdf() && !"1".equals(convertError))
				return true;
		}
		return false;
	}
	@Transient
	public boolean isImage(){
		if("jpg".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		if("jpeg".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		if("gif".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		if("png".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		return false;
	}
	@Transient
	public boolean isConvertedSwf(){
		java.io.File oFile=new java.io.File(getConfig().getSwfFileStoreDirectory()+java.io.File.separator+this.getGuid()+java.io.File.separator+"1.swf");
		if(oFile.exists()){
			return true;
		}
		return false;
	}
	@Transient
	public boolean isMoveToOk(){
		java.io.File batokFile=new java.io.File(config.getConfigHolder().getConfig(
				Constants.FOLDER_BAT)+File.separator+"ok"+File.separator+guid+".bat");
		if(batokFile.exists()){
			return true;
		}
		return false;
	}
	@Transient
	public boolean isSwf(){
		if("swf".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		return false;
	}
	@Transient
	public boolean isText(){
		if("txt".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		return false;
	}
	@Transient
	public boolean isPdf(){
		if("pdf".equalsIgnoreCase(this.getAttachType().getName()))
			return true;
		return false;
	}
	public boolean isConvertOk() {
		return convertOk;
	}

	public void setConvertOk(boolean convertOk) {
		this.convertOk = convertOk;
	}
	
	private static FileAttachmentRepository fileAttachmentRepository;
	
	public static FileAttachmentRepository fileAttachmentRepository() {
		if (fileAttachmentRepository == null) {
			fileAttachmentRepository = InstanceFactory.getInstance(FileAttachmentRepository.class);
		}
		return fileAttachmentRepository;
	}
	
	private static FileManagerConfig config;
	
	public static FileManagerConfig getConfig() {
		if (config == null) {
			config = InstanceFactory.getInstance(FileManagerConfig.class);
		}
		return config;
	}
	
	public void add(){
		fileAttachmentRepository().save(this);
	}
	public void save(){
		fileAttachmentRepository().save(this);
	}
	public void delete() {
		fileAttachmentRepository().remove(this);
	}
	
	public String generateFileName(){
		return this.getGuid().concat(".").concat(this.getAttachType().getName());
	}
	
	public String generateIcon(){
		return this.getGuid().concat("icon.").concat(this.getAttachType().getName());
	}
	
	public String getImagePath(){
		String big=getConfig().getPathBase()+getConfig().getPathImage()+this.getAttachPath()+this.generateFileName();
		String small=getConfig().getPathBase()+getConfig().getPathImage()+this.getAttachPath()+this.generateIcon();
		try {
			java.io.File smallfile=new java.io.File(small);
			java.io.File bfile=new java.io.File(big);
			if(bfile.exists() && !smallfile.exists()){
				ImageZipUtil.zipImageFile(getConfig().getPathBase()+getConfig().getPathImage()+this.getAttachPath(),big,180, 144, 0.75f, "icon");   
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getConfig().getMappingIp()+getConfig().getPathImage()+this.getAttachPath()+this.generateIcon();
	}
	
	public String getPhotoPath(){
		String attachPath=this.getAttachPath();
		String big=getConfig().getPathBase()+getConfig().getPathImage()+attachPath+this.getPhotoName();
		String name=FilenameUtils.getBaseName(this.getPhotoName())+"icon."+FilenameUtils.getExtension(this.getPhotoName());
		String small=getConfig().getPathBase()+getConfig().getPathImage()+attachPath+name;
		try {
			java.io.File smallfile=new java.io.File(small);
			java.io.File bfile=new java.io.File(big);
			if(bfile.exists() && !smallfile.exists()){
				ImageZipUtil.zipImageFile(getConfig().getPathBase()+getConfig().getPathImage()+attachPath,big,190, 190, 0.75f, "icon");   
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getConfig().getMappingIp()+getConfig().getPathImage()+attachPath+name;
	}
}
